﻿var controller = this.lookupController();
var status = controller.analysis().status;
var expenseType = ref.ExpenseType.getValue();

controller.balance = controller.balance || {};
controller.budgetControl = controller.budgetControl || {};

var budgetControl = "N";
var store2 = ref.datagrid2.getStore();

var grid4 = ref.datagrid4;
var grid5 = ref.datagrid5;

if ((status == 'launch' || status == 'resubmit')) {
    var record = store.getUpdatedRecords()[0],
        flag = 'update';

    if (!record) {
        record = store.getNewRecords()[0];
        flag = 'add';
    }

    if (!record) {
        flag = 'reset';
    }

    if (record) {
        //时时抓取Balance数据

        controller.getBalance(record.get("ProjectActivity"));

        if (flag == 'update') {
            if (!Ext.Object.isEmpty(record.modified)) {
                var companySharingRatio = record.get("CompanySharingRatio");

                if (companySharingRatio) {
                    //CompanySharingRatio 不能输入小于0 或大于100的数 否则清空.
                    if (parseFloat(companySharingRatio) < 0 || parseFloat(companySharingRatio) > 100) {
                        record.data.CompanySharingRatio = record.previousValues ? record.previousValues.CompanySharingRatio : 0;
                    }
                }

                var purStore = store2;
                var r = purStore.findRecord('DescriptionOfSuppliesServices', record.data.DescriptionOfSuppliesServices);
                if (r) {
                    record.data.ProjectActivity = r.data.ProjectActivity;
                    record.data.Cost = (r.data.TotalPrice * record.data.CompanySharingRatio) / 100;
                }

                var _store = record.data.Company === 'JLRC' ? grid4.getStore() : grid5.getStore();
                for (var i = 0; i < _store.data.items.length; i++) {
                    var item = _store.data.items[i];
                    if (item.get('DescriptionOfSuppliesServices') == record.data.DescriptionOfSuppliesServices) {

                        if (item.data.JASharingRatio !== undefined) {
                            var companyCost = record.data.Cost;
                            item.data.JACost = parseFloat(((companyCost * item.data.JASharingRatio) / 100).toFixed(2));
                            item.data.LRCost = companyCost - item.data.JACost;
                            item.data.ProjectActivity = record.data.ProjectActivity;
                            item.commit();
                        }
                    }
                }
            }
            record.commit();
        } else if (flag == 'add') {
            var gridRecordList = ref.datagrid2.getStore().data.items;

            if (ref.ExpenseType.getValue() == '6' && gridRecordList.length == 1) {
                setTimeout(function () {
                    record.set('DescriptionOfSuppliesServices', gridRecordList[0].get('DescriptionOfSuppliesServices'));
                }, 200);
            }

            record.commit();
        }

        // CJLR company sharing ratio = 100 %, Finance Analyst Owner（JLRC）选人框自动隐藏
        // JLRC company sharing ratio = 100 %, Finance Analyst Owner（CJLR）选人框自动隐藏
        var ratio = record.get('CompanySharingRatio');
        var company = record.get('Company');
        if (company != 'LRE') {

            if (company == 'JLRC') {
                if (ratio == 100) {
                    ref['financeAnalystOwnerCJLR'].hide();
                } else {
                    ref['financeAnalystOwnerCJLR'].show();
                }
            } else if (company == 'CJLR') {
                if (ratio == 100) {
                    ref['financeAnalystOwnerJLRC'].hide();
                } else {
                    ref['financeAnalystOwnerJLRC'].show();
                }
            }
        }
    }
}

//数据变更 需要同步Total
var amoutObj = {
    JLRC: 0,
    CJLR: 0,
    LRE: 0
};

Ext.each(ref.datagrid3.store.data.items, function (record) {
    var company = record.get('Company');

    if (record.get('Cost')) {
        amoutObj[company] = parseFloat((amoutObj[company] + record.get('Cost')).toFixed(2));
    }
});
var layoutColumns = 5;
for (var c in amoutObj) {
    if (c) {
        if (amoutObj[c]) {
            ref['total' + c].show();
            ref['total' + c].setValue(Ext.util.Format.number(amoutObj[c], '0,000.00'));
        } else {
            layoutColumns--;
            ref['total' + c].hide();
        }
    }
}
ref.section44.layoutColumns = layoutColumns;